<?php
                                                                                                                                                                                                                                                                                                                                        
// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      
                                                                                                                                                                     
//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2022/04/21
//      |_|                                         | TpMeCMS

namespace app\api\controller;
use app\api\controller\Tpmecms;

use think\Db;
use EasyWeChat\Factory;


/**
 * 微信公众号
 * @ApiInternal()
 */
class WxTemplate extends Tpmecms
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    public function _initialize()
    {
        parent::_initialize();
    }




    /**
     * 给关注微信公众号用户发送消息
     * @ApiSummary (接口配置信息：网站/api/wechatofficial/message)
     * @return void
     */
    public function send_all_users()
    {
        //微信测试号地址：https://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
        $app = $this->get_wx_gzh_app();
        //获取用户openid
        $res = $app->user->list();
        if($res['total']<1){
            $this->error('一个关注用户也没有');
        }
        $openids = $res['data']['openid'];
        //循环发送
        foreach ($openids as $key => $openid) {
            $this->test_send_user($openid); 
        }
    }

    /**
     * 测试发送微信模版消息给用户
     *
     * @param string $openid 微信公众号openid
     * @return void
     */    
    public function test_send_user($openid)
    {
        $app = $this->get_wx_gzh_app();
        $res = $app->template_message->getPrivateTemplates();
        $list = $res['template_list'];
        // halt($list);
        if(sizeof($list)<1){
            $this->error('还未添加模版哦');
        }
        $template = $list[0];
        
        $msg_res = $app->template_message->send([
            'touser' => $openid,
            'template_id' => $template['template_id'],
            'url' => 'http://wechat.rsws.top/cms/user/wx_login',
            // 'miniprogram' => [
            //         'appid' => 'xxxxxxx',
            //         'pagepath' => 'pages/xxx',
            // ],
            'data' => [
                'content' => '这是发送的测试内容',
                'key2' => 'QQ496631085',
                
            ],
        ]);
        if($msg_res['errcode']>0){
            $this->error($msg_res['errmsg']);
        }

    }
    
    
    
      
     /**
     * 获取所以模版
     *
     * @return void
     */    
    public function get_templates()
    {
        $app = $this->get_wx_gzh_app();
        $res = $app->template_message->getPrivateTemplates();
        $list = $res['template_list'];
        $this->success('模版列表',$list);
    }
    
    
     /**
     * 发送微信模版消息给用户
     *
     * @param string $openid 微信公众号openid
     * @param string $content 内容 
     * @return void
     */    
    public function send_user($openid='oequ652m7_N6H0eQkn7sUz66QtXw',$content='内容')
    {
        $app = $this->get_wx_gzh_app();
        $res = $app->template_message->getPrivateTemplates();
        $list = $res['template_list'];
        // halt($list);
        if(sizeof($list)<1){
            $this->error('还未添加模版哦');
        }
        $template = $list[0];
        
        $msg_res = $app->template_message->send([
            'touser' => $openid,
            'template_id' => '7KQR0JnS0UjgKyQ1tdAE3NPoJxNpUyyF3cIzEqGgpdA',
            'url' => 'http://wechat.rsws.top/cms/user/wx_login',
            'data' => [
                //{{first.DATA}}\n通知内容：{{keyword1.DATA}}\n通知时间：{{keyword2.DATA}}\n{{remark.DATA}}
                'first' => $content,
                 'keyword1' => $content,
                 'keyword2'=> date('Y-m-d H:i:s'),
                 'remark'=> '备注',
                 
                
            ],
        ]);
        if($msg_res['errcode']>0 || $msg_res['errmsg']!='ok'){
            $this->error('(是否关注微信公众号了？)发送失败:'.$msg_res['errmsg']);
        }else {
            $this->success('ok');
        }

    }



}